<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/1/30
 * Time: 18:46
 */
namespace app\admin\controller;
use think\Db;
class TeacherController extends CommonController{

    /**
     *getTeacherData 页面加载获取教师表数据
     * @return json格式数组
     */
    public function getTeacherData(){
//        var_dump($_POST);die;
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $where = $this->getDataByCampusid($_POST);
        $res = Db::table('ew_teacher')
            ->where($where)
            ->field('tea_id,tea_username,tea_name,tea_sex,tea_phone,tea_homework,tea_status')
            ->limit($rows * ($page - 1), $rows)
            ->select();

        $total = Db::table('ew_teacher')
            ->where($where)
            ->field('tea_id,tea_username,tea_name,tea_sex,tea_phone,tea_homework,tea_status')
            ->count();
        $data['total'] = $total;
        $data['rows'] = $res;
        return json_encode($data);
    }

    /**
     * addTeacher 展示添加教师信息页面
     * @return  HTML格式页面
     */
    public function addTeacher(){
        return $this->fetch('Teacher/addTeacher');
    }


    /**
     * insert 收集表单信息插入数据库
     * @return  返回操作数据库状态
     */
    public function insert(){
        extract($_POST);
        /*用户名*/
        if($tea_username == '' || preg_match("/^[a-zA-Z0-9]+$/",$tea_username) == 0){
            $returnData['status'] = 0;
            $returnData['msg'] = "请填写正确用户名！";
            return json_encode($returnData);
        }else{
            $res = Db::table('ew_teacher')->column('tea_username');
            if(in_array($tea_username,$res)){
                $returnData['status'] = 0;
                $returnData['msg'] = "用户名已存在！";
                return json_encode($returnData);
            }
        }
        /*密码*/
        if($tea_password =='' || $again_password == ''){
            $returnData['status'] = 0;
            $returnData['msg'] = "请填写密码！";
            return json_encode($returnData);
        }else if($tea_password !== $again_password){
            $returnData['status'] = 0;
            $returnData['msg'] = "两次填写的密码不一致！";
            return json_encode($returnData);
        }else{
            $tea_password = md5($tea_password);
        }

        /*姓名*/
        if($tea_name == ''){
            $returnData['status'] = 0;
            $returnData['msg'] = "请填写姓名！";
            return json_encode($returnData);
        }

        /*头像*/
        if($_FILES['file']['name']== '') {
            $returnData['status'] = 0;
            $returnData['msg'] = "请为用户添加一个头像！";
            return json_encode($returnData);
        }
        /*电话*/
        if($tea_phone == '' || preg_match("/^1[34578]{1}\d{9}$/",$tea_phone)== 0){
            $returnData['status'] = 0;
            $returnData['msg'] = "请输入正确电话号码！";
            return json_encode($returnData);
        }

        /*待批改作业数*/
        if($tea_homework == '' || $tea_homework < 0){
            $returnData['status'] = 0;
            $returnData['msg'] = "请输入正确待批改作业数！";
            return json_encode($returnData);
        }

        //上传头像
        $file = request()->file('file');
        $info = $file->move(ROOT_PATH . 'static' . DS . 'upload');
        if($info){
            // 成功上传后 获取上传信息
            // 输出 jpg
//            echo $info->getExtension();
            // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
            $SaveName = $info->getSaveName();
            // 输出 42a79759f284b767dfcb2a0197904287.jpg
//            echo $info->getFilename();
        }else{
            // 上传失败获取错误信息
            $returnData['status'] = 0;
            $returnData['msg'] = $file->getError();
            return json_encode($returnData);
        }

        $data = [
            'tea_username'=>$tea_username,
            'tea_password'=>$tea_password,
            'tea_name'=>$tea_name,
            'tea_thumb'=>$SaveName,
            'tea_sex'=>$tea_sex,
            'tea_phone'=>$tea_phone,
            'tea_homework'=>$tea_homework,
            'tea_status'=>$tea_status
        ];
        $res = Db::table('ew_teacher')
            ->insert($data);
        if($res){
            $returnData['status'] = 1;
            $returnData['msg'] = "添加成功！";
            return json_encode($returnData);
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "添加失败！";
            return json_encode($returnData);
        }
    }

    /**
     * updateTeacher 展示添加教师信息修改页面
     * @return  HTML格式页面
     */
    public function updateTeacher(){
        $arr = Db::table('ew_teacher')
            ->where('tea_id',$_POST['nid'])
            ->field('tea_id,tea_username,tea_password,tea_name,tea_thumb,tea_sex,tea_phone,tea_homework,tea_status')
            ->find();
        $this->assign("res",$arr);
       return  $this->fetch('Teacher/updateTeacher');
    }

    /**
     * update 收集教师信息修改页面数据，修改数据库
     * @return  返回数据库修改状态
     */
    public function update(){
        extract($_POST);
        $data = array();
        /*用户名*/
        if($tea_username == ''){
            $returnData['status'] = 0;
            $returnData['msg'] = "请填写用户名！";
            return json_encode($returnData);
        }
        /*密码*/
        if($tea_password !==''){
            $data['tea_password'] = md5($tea_password);
        }

        /*姓名*/
        if($tea_name == ''){
            $returnData['status'] = 0;
            $returnData['msg'] = "请填写姓名！";
            return json_encode($returnData);
        }

        /*电话*/
        if($tea_phone == '' || preg_match("/^1[34578]{1}\d{9}$/",$tea_phone)==false){
            $returnData['status'] = 0;
            $returnData['msg'] = "请输入正确电话号码！";
            return json_encode($returnData);
        }

        /*待批改作业数*/
        if($tea_homework == '' || $tea_homework < 0){
            $returnData['status'] = 0;
            $returnData['msg'] = "请输入正确待批改作业数！";
            return json_encode($returnData);
        }
        //上传头像
        if($_FILES['file']['name'] !==''){
            $file = request()->file('file');
            $info = $file->move(ROOT_PATH . 'static' . DS . 'upload');
            if($info){
                // 成功上传后 获取上传信息
                // 输出 jpg
//            echo $info->getExtension();
                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                $SaveName = $info->getSaveName();
                // 输出 42a79759f284b767dfcb2a0197904287.jpg
//            echo $info->getFilename();
                $data['tea_thumb'] = $SaveName;
            }else{
                // 上传失败获取错误信息
                $returnData['status'] = 0;
                $returnData['msg'] = $file->getError();
                return json_encode($returnData);
            }

        }
        $data['tea_username'] = $tea_username;
        $data['tea_name'] = $tea_name;
        $data['tea_sex'] = $tea_sex;
        $data['tea_phone'] = $tea_phone;
        $data['tea_homework'] = $tea_homework;
        $data['tea_status'] = $tea_status;

        $res = Db::table('ew_teacher')
            ->where('tea_id',$tea_id)
            ->update($data);
        if($res){
            $returnData['status'] = 1;
            $returnData['msg'] = "修改成功！";
            return json_encode($returnData);
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "添加失败！";
            return json_encode($returnData);
        }
    }

    /**
     * disabledTeacher 停用教师
     * @return 返回状态
     */
    public function disabledTeacher(){
        $data = $this->oneuser($_POST['cid'],2,"已停用！","停用失败！");
        return $data;
    }

    /**
     * enableuser 启用教师
     * @return 返回状态
     */
    public function enableTeacher(){
        $data = $this->oneuser($_POST['cid'],1,"已启用！","启用失败！");
        return $data;
    }
    /**
     * alluser 停用或启用单个教师
     * @param、$uid教师id、$num状态值、$succemsg成功返回信息、$failedmsg失败返回信息
     * @return 返回状态
     */
    protected function oneuser($uid,$num,$succemsg,$failedmsg){
        if($uid!=''){
            $where['tea_id'] = $uid;
            $data['tea_status'] = $num;//停用|启用
            $res =  Db::table('ew_teacher')->where($where)->update($data);
            if($res){
                $returnData['status'] = 1;
                $returnData['msg'] = $succemsg;
            }else{
                $returnData['status'] = 0;
                $returnData['msg'] = $failedmsg;
            }
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "传递参数错误";
        }
        return json_encode($returnData);
    }

    /**
     * enablealluser 启用所有教师
     * @return 返回状态
     */
    public function enableallTeacher(){
        $data = $this->alluser(1,2,"已启用所有教师！","启用失败！");
        return $data;
    }

    /**
     * disablealluser 停用所有教师
     * @return 返回状态
     */
    public function disableallTeacher(){
        $data = $this->alluser(2,1,"已停用所有教师！","停用失败！");
        return $data;
    }

    /**
     * alluser 停用或启用所有教师
     * @param $num & $D状态值、$succemsg成功返回信息、$failedmsg失败返回信息
     * @return 返回状态
     */
    protected function alluser($num,$D,$succemsg,$failedmsg){
        $data['tea_status'] = $num;
        $arr = Db::table('ew_teacher')->where('tea_status',$D)->select();
        if($arr != null ){
            $res =  Db::table('ew_teacher')->where("tea_status",$D)->update($data);
            if($res){
                $returnData['status'] = 1;
                $returnData['msg'] = $succemsg;
            }else{
                $returnData['status'] = 0;
                $returnData['msg'] = $failedmsg;
            }
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = $succemsg;
        }

        return json_encode($returnData);
    }
}